define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'orderagency/index' + location.search,
                    add_url: 'orderagency/add',
                    // edit_url: 'order/edit',
                    del_url: 'order/del',
                    audit_url: 'orderagency/audit',
                    recover_url: 'orderagency/recover',
                    multi_url: 'orderagency/multi',
                    import_url: 'orderagency/import',
                    jssb_url:'orderagency/jssb',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), operate: 'LIKE'},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        // {field: 'jiesuan_time', title: __('待结算时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_id', title: __('Order_id'), operate: 'LIKE'},
                        {
                            field: 'packages.user_name',
                            title: __('Packages.name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'packages.supplier',
                            title: __('供应商'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'platform_type',
                            title: __('Platform_type'),
                            searchList: {
                                "1": __('Platform_type 1'),
                                "2": __('Platform_type 2'),
                                "3": __('Platform_type 3'),
                                "4": __('Platform_type 4'),
                                "5": __('Platform_type 5'),
                                "6": __('Platform_type 6'),
                                "7": __('Platform_type 7'),
                                "8": __('Platform_type 8'),
                                "9": __('Platform_type 9'),
                            },
                            formatter: Table.api.formatter.status
                        },
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        // {field: 'is_tripartite_settlement', title: __('三方结算状态'), searchList: {"0":__('未结算'),"1":__('已结算')}, formatter: Table.api.formatter.status},
                        {
                            field: 'is_more',
                            title: __('结算模式'),
                            searchList: {"1": __('单次结算'), "2": __('多次结算'), "3": __('多次结算(已完结)')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'baoxiao_status',
                            title: __('Baoxiao_status'),
                            searchList: {
                                "0": __('Baoxiao_status 0'),
                                "1": __('Baoxiao_status 1'),
                                "2": __('Baoxiao_status 2'),
                                "-1": __('Baoxiao_status -1'),
                                "-2": __('Baoxiao_status -2')
                            },
                            formatter: Table.api.formatter.status,
                            custom:{2:'blue'}
                        },
                        {
                            field: 'bao_text', title: __('报销描述'), operate: false,
                            formatter:function (value, row, index){
                                console.log(value);
                                if(value == '已全部报销'){
                                    return '<span class="text-blue">'+value+'</span>';
                                }else{
                                    return value;
                                }
                            }
                        },
                        {
                            field: 'is_hexiao',
                            title: __('核销状态'),
                            searchList: {
                                0: __('未核销'),
                                1: __('待核销'),
                                2: __('已核销'),
                                3: __('已逾期'),
                                4: __('首月待核销'),
                                5: __('次月待核销'),
                                6: __('首月已逾期'),
                                7: __('次月待核销'),
                                8: __('次月已核销'),
                                9: __('次月已逾期')
                            },
                            formatter: Table.api.formatter.status,
                            custom:{2:'blue',8:'blue',3:'red',6:'red'}
                        },
                        {field: 'jsms_text', title: __('佣金结算'), operate: false,
                            formatter:function (value, row, index){
                                if(value == '已全部结算'){
                                    return '<span class="text-blue">'+value+'</span>';
                                }else{
                                    return value;
                                }
                            }
                        },
                        {
                            field: 'order_status',
                            title: __('Order_status'),
                            searchList: {
                                "0": __('Order_status 0'),
                                "1": __('Order_status 1'),
                                "2": __('Order_status 2'),
                                "3": __('Order_status 3'),
                                "4": __('Order_status 4'),
                                "5": __('Order_status 5'),
                                "6": __('Order_status 6')
                            },
                            formatter: Table.api.formatter.status
                        },
                        {field: 'remark', title: __('订单备注'), operate: 'LIKE'},
                        {
                            field: 'image_text',
                            title: __('上传截图'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            operate: false
                        },
                        {
                            field: 'call_image_text',
                            title: __('通话记录'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            operate: false
                        },
                        {
                            field: 'audit_upload_time',
                            title: __('上传时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'audit_time',
                            title: __('审核时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'user.username', title: __('下单人'), operate: 'LIKE'},
                        {field: 'parent_name_text', title: __('上级名称'), operate: false},
                        {field: 'area_name_text', title: __('区县运营商名称'), operate: false},
                        {field: 'xiaoqu_text', title: __('小区运营商名称'), operate: false},
                        {field: 'shipping_contact', title: __('Shipping_contact'), operate: 'LIKE'},
                        {field: 'shipping_idcard', title: __('Shipping_idcard'), operate: 'LIKE'},
                        {field: 'shipping_phone', title: __('Shipping_phone'), operate: 'LIKE'},
                        {
                            field: 'shipping_province',
                            title: __('Shipping_province'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'shipping_city',
                            title: __('Shipping_city'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'shipping_area',
                            title: __('Shipping_area'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'shipping_address',
                            title: __('Shipping_address'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'shipping_company_name',
                            title: __('Shipping_company_name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'shipping_no',
                            title: __('Shipping_no'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },

                        {field: 'error_msg', title: __('结算失败')},
                        // {field: 'audit_status', title: __('Audit_status'), searchList: {"0":__('Audit_status 0'),"1":__('Audit_status 1'),"2":__('Audit_status 2'),"3":__('Audit_status 3')}, formatter: Table.api.formatter.status},
                        //
                        // {field: 'recover_status', title: __('Recover_status'), searchList: {"1":__('Recover_status 1'),"2":__('Recover_status 2')}, formatter: Table.api.formatter.status},
                        //
                        //
                        //
                        //
                        //
                        // {field: 'user_rebate', title: __('User_rebate'), operate:'BETWEEN'},
                        // {field: 'is_recharged', title: __('Is_recharged'), searchList: {"0":__('Is_recharged 0'),"1":__('Is_recharged 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'callback_time', title: __('Callback_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        // {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('审核'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                    url: 'orderagency/audit',
                                    visible: function (row) {
                                        // return true;
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.audit_status != 2) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: __('审核'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-success btn-xs btn-detail btn-dialog',
                                    url: 'orderagency/audit',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.audit_status == 2) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: __('修改订单'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'orderagency/edit'
                                },
                                {
                                    name: '结算失败',
                                    text: __('结算失败'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                    url: 'orderagency/jssb',
                                    visible:function (row){
                                        if(!row.image_text && row.audit_status != 2 && row.recover_status == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: '结算失败',
                                    text: __('结算失败'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-warning btn-xs btn-detail btn-dialog',
                                    url: 'orderagency/jssb',
                                    visible:function (row){
                                        if(row.recover_status == 2 && row.rebate_recover_msg){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: '结算失败',
                                    text: __('结算失败'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-danger btn-xs btn-detail btn-dialog',
                                    url: 'orderagency/jssb',
                                    visible:function (row){
                                        if(row.image_text && row.error_msg && row.audit_status != 2){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                // {
                                //     name: '结算失败',
                                //     text: __('结算失败'),
                                //     icon: 'fa fa-list',
                                //     classname: 'btn btn-success btn-xs btn-detail btn-dialog',
                                //     url: 'orderagency/jssb',
                                //     visible:function (row){
                                //         if(row.audit_status == 2){
                                //             return true;
                                //         }else{
                                //             return false;
                                //         }
                                //     }
                                // },

                                {
                                    name: '结算失败',
                                    text: __('结算失败'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                    url: 'orderagency/jssb',
                                    visible:function (row){
                                        if(row.audit_status == 2){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                // {
                                //     name: 'detail',
                                //     text: __('追回佣金'),
                                //     icon: 'fa fa-list',
                                //     classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                //     url: 'orderagency/recover',
                                //     visible: function (row) {
                                //         //返回true时按钮显示,返回false隐藏
                                //         return true;
                                //         // if(row.recover_status==1 && row.recover_status==5){
                                //         //     return true;
                                //         // }else{
                                //         //     return false;
                                //         // }
                                //     },
                                // },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        audit: function () {
            Controller.api.bindevent();
        },
        recover: function () {
            Controller.api.bindevent();
        },
        orderinfo: function () {
            Controller.api.bindevent();
        },
        orderchuli: function () {
            Controller.api.bindevent();
        },
        hexiao: function () {
            Controller.api.bindevent();
        },
        jssb:function (){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
